 <?
/* Copyright (c) 2008-2011 Submit Consulting. Lima-Peru. All Right Reserved.
 * @author Ing. Angel Sullon M. (asullom@hotmail.com)
 * Created: 30.SEP.2010
 * Last Modify: 11.AGO.2011
 * Description of ConnectDb: Permite conenctar con la DB MySQL
 */
require_once 'CryptoString.php';

class ConnectDb {
    private $conn=null;
    private $server;
    private $username;
    private $password;
    private $databaseName;

    function __construct(){
	$this->server = Config::$SERVER ;
	$this->username = Config::$USERNAME ;
    $this->password = Config::$PASSWORD ;
    $this->databaseName = Config::$DBNAME ;
    }

    public function  getConnection() {
            $crypto= new CryptoString();
            if(!$this->conn)
                try{
                    $this->conn=mysql_connect($this->server, $this->username, $this->password )  or die("<p id=Error>".mysql_error()." </p>");
                    //$this->conn=mysql_connect("localhost", "root", "" ) or die("<p id=error>".mysql_error()." </p>");
                    mysql_select_db( $this->databaseName);
                    /* check connection */
                    if (!$this->conn) {
                        throw new Exception("Connect failed: ". mysql_error());
                        exit();
                    }

                    //if($this->conn) print("Connection. ");
                }catch(Exception $e){
                    throw new Exception("ConnectDb.getConnection ".$e->getMessage()." from ".$this->host_port." try again ");
                }
            return $this->conn;
    }


    public function close() {

        try{
            if($this->conn) {
                mysql_close($this->conn);
                $this->conn=null;
                //print("Close. ");
            }
        }catch(Exception $e){
            throw new Exception("ConnectDb.close ".$e->getMessage()." ");
        }
    }

    	public function begin(){
            mysql_query("BEGIN;", $this->conn);
	}
        public function commit(){
            mysql_query("COMMIT;", $this->conn);
	}
    	public function rollback(){
            mysql_query("ROLLBACK;", $this->conn);
	}



    public function mainx() {

          $sql=new ConnectDb();
            try{
                    $sql->getConnection();
                    $sql->getConnection();
                    $sql->close();
                    $sql->close();
                    $sql->getConnection();
                    $sql->close();
                    $sql->close();
            }catch(Exception $e){
                    print("e:".$e->getMessage() );
            }

    }
}

?>